import React from 'react';

export default class WorkBillList extends React.Component {
  constructor(props) {
    super(props)
  }

  pad(str, length) {
    str = str + ""
    if (str.length == length) {
      return str
    }
    return this.pad("0"+str, length)
  }

  renderList() {
    var self = this;
    var content = [];
    if (this.props.data) {
      this.props.data.forEach(function(data, index) {

        var print_date = data.print_date;
        var print = print_date?(new Date(print_date)).toLocaleTimeString():"";
        var pushText = data.is_push?"已推送":"推送"
        var amount = 0
        if (data.property_bill_lines && data.property_bill_lines.length > 0) {
          for (var i = 0; i < data.property_bill_lines.length; i++) {
            var billLine = data.property_bill_lines[i]
            amount += parseFloat(billLine.gross_amount)
          }
        }
        var pay = amount.toFixed(2) + "元"
        var desc = "";
        if (data.unit.length > 0) {
          desc = data.unit[0].unit_desc
        }

        var start_date = "",
            end_date = ""
        if (data.created_at) {
          var startDate = new Date(data.created_at)
          start_date = startDate.getFullYear() + "-" +
                      self.pad(startDate.getMonth()+1,2) + "-" + self.pad(startDate.getDate(),2)
        }

        var pay_type = ""
        if (data.pay_type == 5) {
          pay_type = "微信支付"
        }else if (data.pay_type == 10) {
          pay_type = "线下支付"
        }else if (data.pay_type == 15) {
          pay_type = "月结"
        }

        content.push((
          <tr>
            <td>{data.bill_number}</td>
            <td>{data.jde_order_number}</td>
            <td>{desc}</td>
            <td>{data.username}</td>
            <td>{start_date}</td>
            <td>{pay}</td>
            <td>{pay_type}</td>
            <td>
              <a onClick={(event)=>self.props.onEditClick(index)}>详情</a>
              {
                // <a className = "pushMsg" onClick={(event)=>self.props.onPushMessageClick(data)}>{pushText}</a>
              }
            </td>
          </tr>
        ))
      })
    }

    return content
  }

  render() {
    return(
      <div className="bill-setting-list setting-list">
        <table className="table ">
          <thead>
            <tr className="table-title">
              <th>账单号</th>
              <th>维修单号</th>
              <th>户号</th>
              <th>业主</th>
              <th>维修日期</th>
              <th>费用</th>
              <th>付款方式</th>
              <th>详情</th>
            </tr>
          </thead>
          <tbody>
            {this.renderList()}
          </tbody>
        </table>
      </div>
    )
  }
}
